﻿using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Security;
using Hutchinson.CustomField.Multichoice.MCCustomizable;
using System.Reflection;
using System.Xml;
using System.Collections.Specialized;
using System.IO;
using Microsoft.SharePoint.Utilities;
using System.Resources;

namespace Hutchinson.CustomField.Multichoice
{

    public enum VisualTemplateType
    {
        MCCustomizableControlCheck = 1,
        MCCustomizableControlRadio = 2,
    }

    [CLSCompliant(false)]
    [Guid("6552a9ed-b6dc-4065-8719-04a8ba5d39e9")]
    public class MCCustomizableField : SPFieldMultiChoice
    {

        #region Propriétés


        string _repeatDirection;
        public string RepeatDirection
        {
            get
            {
                return GetFieldAttributeValue("RepeatDirection");
            }
            set
            {
                _repeatDirection = value;
                SetFieldAttributeValue("RepeatDirection", _repeatDirection);
            }
        }

        int _nombreColonnes;
        public int NombreColonnes
        {
            get
            {
                return int.Parse(GetFieldAttributeValue("NombreColonnes"));
            }
            set
            {
                _nombreColonnes = value;
                SetFieldAttributeValue("NombreColonnes", _nombreColonnes);
            }
        }

        string _strVisualTemplateType = null;
        VisualTemplateType _VisualTemplateType;
        public VisualTemplateType ShowType
        {
            get
            {
                if (_strVisualTemplateType == null)
                {
                    _strVisualTemplateType = GetFieldAttributeValue("ShowType");
                    if (_strVisualTemplateType == null)
                        _strVisualTemplateType = VisualTemplateType.MCCustomizableControlCheck.ToString();

                    _VisualTemplateType = (VisualTemplateType)Enum.Parse(typeof(VisualTemplateType), _strVisualTemplateType);
                }
                return _VisualTemplateType;
            }
            set
            {
                _VisualTemplateType = value;
                SetFieldAttributeValue("ShowType", _VisualTemplateType.ToString());
            }
        }

        #endregion


        public MCCustomizableField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
        }

        public MCCustomizableField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
        }

        public override object GetFieldValue(string value)
        {
            if (string.IsNullOrEmpty(value))
                return null;
            return new SPFieldMultiChoiceValue(value);
        }


        public void SetFieldAttributeValue(string property, object value)
        {
            Type t = typeof(MCCustomizableField);
            MethodInfo mi = t.GetMethod("SetFieldAttributeValue", BindingFlags.Instance | BindingFlags.NonPublic);
            object ret = mi.Invoke(this, new object[] { property, value.ToString() });
        }

        public string GetFieldAttributeValue(string property)
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(this.SchemaXml);
            XmlElement documentElement = document.DocumentElement;
            if (documentElement.Attributes[property] != null)
                return documentElement.Attributes[property].Value;
            else
                return null;
        }


        public override BaseFieldControl FieldRenderingControl
        {
            [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            get
            {
                BaseFieldControl fieldControl = null;

                switch (this.ShowType)
                {
                    case VisualTemplateType.MCCustomizableControlRadio:
                        fieldControl = new MCCustomizableControlRadio();
                        break;
                    default:
                        fieldControl = new MCCustomizableControlCheck();
                        break;
                }

                fieldControl.FieldName = this.InternalName;

                return fieldControl;
            }
        }

        public void UpdateChoices()
        {
            this.UpdateInternal();
        }

        internal string cleanValue(string value2Select)
        {
            string sep = ";#";
            if (value2Select.Contains(sep))
                return value2Select.Split(sep.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[0];
            else
                return value2Select.Trim();
        }

        internal string GetRessources(string Key)
        {
            string name ="Hutchinson.CustomField.Multichoice";
            string head = string.Format("$Resources:{0},{1}",name,Key);
            string mess = SPUtility.GetLocalizedString(head, name, SPContext.Current.Web.Language); 
            return mess;
        }
    }
}